package flown.window.graphics.widget;

import flown.window.graphics.Renderer2D;
import flown.window.graphics.widget.event.ClickEvent;
import flown.window.graphics.widget.listener.ClickListener;

import java.awt.Rectangle;


/**
 * @author flown
 */

public class Widget
{
    protected Rectangle rect;///< x,y,width,height
    private ClickEvent click_event = ()->{};

    public Widget() { this(0, 0, 0, 0); }
    public Widget(int width, int height) { this(0, 0, width, height); }
    public Widget(int x, int y, int width, int height)
    {
        this.SetBounds(x, y, width, height);
    }

    public final void SetBounds(int x, int y, int width, int height)
    {
        this.rect = new Rectangle();
        this.rect.x = x;
        this.rect.y = y;
        this.rect.width = width;
        this.rect.height = height;
    }

    public final int GetX() { return this.rect.x; }
    public final int GetY() { return this.rect.y; }
    public final int GetWidth() { return this.rect.width; }
    public final int GetHeight() { return this.rect.height; }


    /**
     * 设置click事件
     */
    public final void SetClickListener(ClickEvent event)
    {
        this.click_event = event;
    }


    /**
     *
     * @param widget Widget
     * @return 是否相交
     */
    public final boolean Intersects(Widget widget) { return this.rect.intersects(widget.rect); }

    /**
     *
     * @param r Rectangle
     * @return 是否相交
     */
    public final boolean Intersects(Rectangle r) { return this.rect.intersects(r); }

    /**
     *
     * @param x 横坐标
     * @param y 纵坐标
     * @param width 宽
     * @param height 高
     * @return 是否相交
     */
    public final boolean Intersects(int x, int y, int width, int height)
    {
        return this.rect.intersects(new Rectangle(x, y, width, height));
    }


    public void Render(Renderer2D renderer) {}

}



